package com.news.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.news.entity.News;
import com.news.entity.User;
import com.news.service.NewsService;
import com.news.util.PageModel;

@Controller
@RequestMapping("/system/news")
public class NewsController {

	@Resource(name = "newsManager")
	private NewsService newsService;

	@RequestMapping("/getindex")
	public String getindex(HttpServletRequest request) {

		return "/index";
	}

	// toaddNews
	@RequestMapping("/toaddnews")
	public String toaddNews() {
		return "/addnews";
	}

	@RequestMapping("/toindex")
	public String toindex() {
		return "/index";
	}

	@RequestMapping("/tomynews")
	public String tomyNews() {
		return "/mynews";
	}

	@RequestMapping("/toservice")
	public String toservice() {
		return "/services";
	}

	// addNews
	@RequestMapping(value = "/addnews", method = RequestMethod.POST)
	public String addnews(
			@RequestParam(value = "file", required = false) MultipartFile file,
			News news, HttpServletRequest request, HttpSession httpSession) {

		User user = (User) httpSession.getAttribute("USER");
		news.setP_id(user.getUserid());

		String path = request.getSession().getServletContext()
				.getRealPath("images");
		String fileName = file.getOriginalFilename();
		news.setUrlpicture("images/" + fileName);
		newsService.addNews(news);
		File targetFile = new File(path, fileName);

		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// ����
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 下载
		request.setAttribute("file", request.getContextPath() + "/images/"
				+ fileName);

		return "/index";
	}

	@RequestMapping("/getAllNews")
	@ResponseBody
	public PageModel getAllNews(HttpServletRequest request) {

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		PageModel pageModel = newsService.getPageModel(page, 4);
		// request.setAttribute("pageModel", pageModel);

		return pageModel;
	}

	@RequestMapping("/getAllNewsByPid")
	@ResponseBody
	public PageModel getAllNewsBypid(HttpServletRequest request,
			HttpSession httpSession) {
		User user = (User) httpSession.getAttribute("USER");
		int pid = user.getUserid();

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		PageModel pageModel = newsService.getPageModelByPid(page, 4, pid);
		// request.setAttribute("pageModel", pageModel);

		return pageModel;
	}

	@RequestMapping("/delNews")
	public void delUser(int newsid, HttpServletResponse response) {

		String result = "{\"result\":\"error\"}";

		if (newsService.delNews(newsid)) {
			result = "{\"result\":\"success\"}";
		}

		response.setContentType("application/json");

		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// id
	@RequestMapping("/getNews")
	public String getNews(int newsid, HttpServletRequest request) {
		request.setAttribute("news", newsService.getNews(newsid));

		return "/updatenews";
	}

	// updateUser
	@RequestMapping(value = "/updateNews", method = RequestMethod.POST)
	public String updateNews(
			@RequestParam(value = "file", required = false) MultipartFile file,
			News news, HttpServletRequest request, HttpSession httpSession) {

		User user = (User) httpSession.getAttribute("USER");
		news.setP_id(user.getUserid());

		String path = request.getSession().getServletContext()
				.getRealPath("images");
		String fileName = file.getOriginalFilename();

		news.setUrlpicture("images/" + fileName);

		File targetFile = new File(path, fileName);

		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		// ����
		try {
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 下载
		request.setAttribute("file", request.getContextPath() + "/images/"
				+ fileName);
		news.setC_id(news.getC_id());
		if (newsService.updateNews(news)) {

			news = newsService.getNews(news.getId());
			// request.setAttribute("user", user);
			return "/mynews";
		}
		return "/error";

	}

}
